package com.king.photo.activity;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.king.photo.util.Bimp;
import com.king.photo.util.HttpRequest;

/**
 * 首页面activity
 * 
 * @author king
 * @QQ:595163260
 * @version 2014年10月18日 下午11:48:34
 */
public class MainActivity extends Activity {

	private PopupWindow pop = null;
	private LinearLayout ll_popup;
	public static Bitmap bimap;
	private Handler mHandler = new Handler();
	WebView webView ;
	@SuppressWarnings("deprecation")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		webView = new WebView(this);
		setContentView(webView);
		webView.setVerticalScrollBarEnabled(false);
		webView.setHorizontalScrollBarEnabled(false);
		webView.getSettings().setJavaScriptEnabled(true); // 加上这句话才能使用javascript方法
		webView.getSettings().setDomStorageEnabled(true);
		webView.requestFocus();
		webView.getSettings().setUseWideViewPort(true);
		webView.getSettings().setLoadWithOverviewMode(true);
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setSavePassword(false);
		webView.getSettings().setSaveFormData(false);
		webView.getSettings().setSupportZoom(false);
		webView.addJavascriptInterface(new JsCallAndroid(), "demo");
		webView.setWebChromeClient(new WebChromeClient());

		
		webView.loadUrl("http://www.17smile.cn/test/views/index.html#/classStyle_tea/publish");
	}

	private static final int TAKE_PICTURE = 0x000001;

	class JsCallAndroid {

		JsCallAndroid() {
		}

		@JavascriptInterface
		public void takePhoto(String type, String callback, String path) {
			startActivity(new Intent(MainActivity.this,
					SelectPicPopupWindow.class));
			if (!path.equals("")) {

				Bimp.tempSelectBitmap.clear();

			}
			SharedPreferences prefs = PreferenceManager
					.getDefaultSharedPreferences(MainActivity.this);
			Editor e = prefs.edit();
			e.putString("path", path);
			e.commit();
			webView.setWebViewClient(new WebViewClient() {
	 			@Override
	public void onPageFinished(WebView view, String url) {
		super.onPageFinished(view, url);
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(MainActivity.this);
		boolean b = prefs.getBoolean("finish", false);
		final String path= prefs.getString("paths",null);
		if (b == true) {
			
			webView.post(new Runnable() {
				@Override
				public void run() {
					webView.loadUrl("javascript:callback(" + "'" +path
							+ "'" + ")");
					
				}
			});
			Editor e = prefs.edit();
			e.putBoolean("finish", false);
			e.commit();

		}
	}
});
		}

		@JavascriptInterface
		public void previewPhoto() {
			if (Bimp.tempSelectBitmap.size() > 0) {
				Intent intent = new Intent();
				intent.putExtra("position", "1");
				intent.setClass(MainActivity.this, GalleryActivity.class);
				startActivity(intent);
			}
		}

		// 上传图片
		@JavascriptInterface
		public String getPhoto(final String list) {
			StringBuffer sb = new StringBuffer();
			final String[] paths = list.split(",");
			new Thread() {
				public void run() {
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < paths.length; i++) {
						String urlPath = "http://www.17smile.cn/app/uploadimagefile";
						HttpRequest request = HttpRequest.post(urlPath);
						request.part("fileType", "message");
						request.part("flag", "image");
						request.part("imageName", paths[i].split("?")[0]);
						request.part("uploadimage", new File("/sdcard/Photo_LJ/"
								+ paths[i].split("Thumb")[1]));
						sb.append( paths[i].split("?")[0]+",");
					}
					
					
				}
			}.start();
			String s = sb.substring(0, sb.length() - 1);
			return s;
		
		}
	}	
	

}
